#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif

#include <SDL/SDL.h>

int main ( int argc, char** argv )
{   int b;
    int cp=1,page=1,cpt=0;
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    atexit(SDL_Quit);

    SDL_Surface* screen = SDL_SetVideoMode(1000, 499, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }
    SDL_Surface* bmp;
    SDL_Surface* menu1 = SDL_LoadBMP("motus1.bmp");
    SDL_Surface* menu2 = SDL_LoadBMP("motus2.bmp");
    SDL_Surface* menu3 = SDL_LoadBMP("motus3.bmp");
    SDL_Surface* menu4 = SDL_LoadBMP("motus4.bmp");
    SDL_Surface* menu5 = SDL_LoadBMP("motus5.bmp");
    SDL_Surface* facile = SDL_LoadBMP("jeu1.bmp");
    SDL_Surface* moyenne = SDL_LoadBMP("jeu2.bmp");
    SDL_Surface* perdu = SDL_LoadBMP("perdu.bmp");
    SDL_Surface* gagne = SDL_LoadBMP("gagne.bmp");
    SDL_Surface* dificile = SDL_LoadBMP("jeu3.bmp");
    SDL_Surface* a = SDL_LoadBMP("a.bmp");
    SDL_Surface* b = SDL_LoadBMP("b.bmp");
    SDL_Surface* c = SDL_LoadBMP("c.bmp");
    SDL_Surface* d = SDL_LoadBMP("d.bmp");
    SDL_Surface* e = SDL_LoadBMP("e.bmp");
    SDL_Surface* f = SDL_LoadBMP("f.bmp");


    if (!menu1 || !menu2 || !menu3|| !menu4|| !menu5 || !facile|| !perdu||!gagne)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Rect dstmenu1;
    dstmenu1.x = 0;
    dstmenu1.y = 0;
    SDL_Rect dst54;
    dst54.x = 608;
    dst54.y = 255;
    int v54 =0,v55=0,v56=0,v57=0,v58=0,v59=0,i,j=0,hhh=0;
    SDL_Rect dst66;
    dst66.x = 660;
    dst66.y = 255;
    SDL_Rect ttt[6];
     if (hhh==1)
                        {

                            ttt[0]= {608,255};
                            ttt[1]= {660,255};
                            ttt[2]= {762,255};
                            ttt[3]= {813,255};
                            ttt[4]= {864,255};
                            ttt[5]= {915,255};
                        }




    bool done = false;
    while (!done)
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                done = true;
                break;

            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE)
                {
                    if (page==1)
                        done = true;
                    else if (page==2)
                        page=1;
                    else if (page==3||page==4||page==5)
                    {
                        page=2;
                        v54=0;
                        v55=0;
                        v56=0;
                        v57=0;
                        v58=0;
                        v59=0;
                    }
                    else if(page==7||page==6)
                    {page=1;
                     v54=0;
                        v55=0;
                        v56=0;
                        v57=0;
                        v58=0;
                        v59=0;}
                }
                else  if (event.key.keysym.sym == SDLK_q)
                {
                    v54 = 1;
                }
                else  if (event.key.keysym.sym == SDLK_b)
                {
                    v55 = 1;
                }
                else if (event.key.keysym.sym == SDLK_c)
                {
                    v56 = 1;
                }
                else if (event.key.keysym.sym == SDLK_d)
                {
                    v57 = 1;
                }
                else if (event.key.keysym.sym == SDLK_e)
                {
                    v58 = 2;
                }
                else if (event.key.keysym.sym == SDLK_f)
                {
                    v59 = 1;
                }
                else if (event.key.keysym.sym == SDLK_DOWN)
                {
                    if (page==1&&cp==0)
                    {
                        bmp=menu1;
                        menu1=menu2;
                        menu2=bmp;
                        cp++;
                    }
                    if (page==2&&cpt==0)
                    {
                        bmp=menu3;
                        menu3=menu4;
                        menu4=bmp;
                        cpt++;
                    }
                    else if (page==2&&cpt==1)
                    {
                        bmp=menu3;
                        menu3=menu5;
                        menu5=bmp;
                        cpt++;
                    }
                }
                else if (event.key.keysym.sym == SDLK_UP)
                {
                    if (page==1&&cp==1)
                    {
                        bmp=menu2;
                        menu2=menu1;
                        menu1=bmp;
                        cp--;
                    }
                    if (page==2&&cpt==2)
                    {
                        bmp=menu3;
                        menu3=menu5;
                        menu5=bmp;
                        cpt--;
                    }
                    else if (page==2&&cpt==1)
                    {
                        bmp=menu3;
                        menu3=menu4;
                        menu4=bmp;
                        cpt--;
                    }
                }
                else if (event.key.keysym.sym == SDLK_RETURN)
                {
                    if (page==1&&cp==0)
                        page=2;
                    else if (page==1&&cp==1)
                        done = true;
                    else   if (page==2&&cpt==0)
                    {
                        page=3;
                        hhh=1;
                    }
                    else   if (page==2&&cpt==1)
                    {
                        page=4;
                        hhh=1;
                    }
                    else   if (page==2&&cpt==2)
                    {
                        page=5;
                        hhh=1;
                    }
                    else
                    {
                        if((page==3||page==4||page==5)&&v54==1&&v55==1&&v56==1&&v57==1&&v58==1&&v59==1)
                            page=6;
                        else
                        {
                            v54=0;
                            v55=0;
                            v56=0;
                            v57=0;
                            v58=0;
                            v59=0;
                            hhh++;
                            if (hhh==7)
                                page=7;
                        }

                        if (hhh==2)
                        {
                            ttt[0]= {608,306};
                            ttt[1]= {660,306};
                            ttt[2]= {762,306};
                            ttt[3]= {813,306};
                            ttt[4]= {864,306};
                            ttt[5]= {915,306};
                        }
                        else  if (hhh==3)
                        {
                            ttt[0]= {608,357};
                            ttt[1]= {660,357};
                            ttt[2]= {762,357};
                            ttt[3]= {813,357};
                            ttt[4]= {864,357};
                            ttt[5]= {915,357};
                        }
                        else  if (hhh==4)
                        {
                            ttt[0]= {608,408};
                            ttt[1]= {660,408};
                            ttt[2]= {762,408};
                            ttt[3]= {813,408};
                            ttt[4]= {864,408};
                            ttt[5]= {915,408};
                        }
                        else  if (hhh==5)
                        {
                            ttt[0]= {608,459};
                            ttt[1]= {660,459};
                            ttt[2]= {762,459};
                            ttt[3]= {813,459};
                            ttt[4]= {864,459};
                            ttt[5]= {915,459};
                        }
                        else  if (hhh==6)
                        {
                            ttt[0]= {608,510};
                            ttt[1]= {660,510};
                            ttt[2]= {762,510};
                            ttt[3]= {813,510};
                            ttt[4]= {864,510};
                            ttt[5]= {915,510};
                        }
                    }
                }


                break;
            }
            }
        }


        if (page==1)
        {
            SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
            SDL_Surface* screen = SDL_SetVideoMode(1000, 499, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(menu1, 0, screen, &dstmenu1);
        }
        if (page==2)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1000, 499, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(menu3, 0, screen, &dstmenu1);
        }
        if (page==3)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1000, 576, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(facile, 0, screen, &dstmenu1);


            j=0;
            if(v54==1)
            {
                SDL_BlitSurface(a, 0, screen, &ttt[j]);
                j++;
            }
            if(v55==1)
            {
                SDL_BlitSurface(b, 0, screen, &ttt[j]);
                j++;
            }
            if(v56==1)
            {
                SDL_BlitSurface(c, 0, screen, &ttt[j]);
                j++;
            }
            if(v57==1)
            {
                SDL_BlitSurface(d, 0, screen, &ttt[j]);
                j++;
            }
            if(v58==2)
            {
                SDL_BlitSurface(e, 0, screen, &ttt[j]);
                j++;
            }
            if(v59==1)
            {
                SDL_BlitSurface(f, 0, screen, &ttt[j]);
                j++;
            }

        }
        if (page==4)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1000, 576, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(moyenne, 0, screen, &dstmenu1);
            j=0;
            if(v54==1)
            {
                SDL_BlitSurface(a, 0, screen, &ttt[j]);
                j++;
            }
            if(v55==1)
            {
                SDL_BlitSurface(b, 0, screen, &ttt[j]);
                j++;
            }
            if(v56==1)
            {
                SDL_BlitSurface(c, 0, screen, &ttt[j]);
                j++;
            }
            if(v57==1)
            {
                SDL_BlitSurface(d, 0, screen, &ttt[j]);
                j++;
            }
            if(v58==2)
            {
                SDL_BlitSurface(e, 0, screen, &ttt[j]);
                j++;
            }
            if(v59==1)
            {
                SDL_BlitSurface(f, 0, screen, &ttt[j]);
                j++;
            }
        }
        if (page==5)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1000, 576, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(dificile, 0, screen, &dstmenu1);
            j=0;
            if(v54==1)
            {
                SDL_BlitSurface(a, 0, screen, &ttt[j]);
                j++;
            }
            if(v55==1)
            {
                SDL_BlitSurface(b, 0, screen, &ttt[j]);
                j++;
            }
            if(v56==1)
            {
                SDL_BlitSurface(c, 0, screen, &ttt[j]);
                j++;
            }
            if(v57==1)
            {
                SDL_BlitSurface(d, 0, screen, &ttt[j]);
                j++;
            }
            if(v58==2)
            {
                SDL_BlitSurface(e, 0, screen, &ttt[j]);
                j++;
            }
            if(v59==1)
            {
                SDL_BlitSurface(f, 0, screen, &ttt[j]);
                j++;
            }
        }
        if (page==6)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1000, 499, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(gagne, 0, screen, &dstmenu1);

        }
        if (page==7)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1000, 499, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(perdu, 0, screen, &dstmenu1);
        }
        SDL_Flip(screen);
    }


    SDL_FreeSurface(bmp);

    printf("Exited cleanly\n");
    return 0;
}
